
clear all;close all;clc

% 'File name.xlsx' for "dominant & non-dominant" in "Dataset Folder";
%  R for dominant side; L for non-dominant side;
% "temp_all" is the result of the crp curve for all participants at different movement distances;

Data1 = importdata('MAX_S1_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data2 = importdata('MAX_S2_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data3 = importdata('MAX_S3_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data4 = importdata('MAX_S4_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data5 = importdata('MAX_S5_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data6 = importdata('MAX_S6_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data7 = importdata('MAX_S7_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data8 = importdata('MAX_S8_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data9 = importdata('MAX_S9_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data10 = importdata('MAX_S10_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data11 = importdata('MAX_S11_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data12 = importdata('MAX_S12_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data13 = importdata('MAX_S13_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data14 = importdata('MAX_S14_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data15 = importdata('MAX_S15_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data16 = importdata('MAX_S16_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data17 = importdata('MAX_S17_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data18 = importdata('MAX_S18_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data19 = importdata('MID_S1_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data20 = importdata('MID_S2_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data21 = importdata('MID_S3_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data22 = importdata('MID_S4_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data23 = importdata('MID_S5_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data24 = importdata('MID_S6_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data25 = importdata('MID_S7_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data26 = importdata('MID_S8_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data27 = importdata('MID_S9_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data28 = importdata('MID_S10_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data29 = importdata('MID_S11_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data30 = importdata('MID_S12_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data31 = importdata('MID_S13_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data32 = importdata('MID_S14_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data33 = importdata('MID_S15_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data34 = importdata('MID_S16_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data35 = importdata('MID_S17_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data36 = importdata('MID_S18_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data37 = importdata('MIN_S1_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data38 = importdata('MIN_S2_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data39 = importdata('MIN_S3_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data40 = importdata('MIN_S4_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data41 = importdata('MIN_S5_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data42 = importdata('MIN_S6_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data43 = importdata('MIN_S7_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data44 = importdata('MIN_S8_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data45 = importdata('MIN_S9_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data46 = importdata('MIN_S10_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data47 = importdata('MIN_S11_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data48 = importdata('MIN_S12_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data49 = importdata('MIN_S13_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data50 = importdata('MIN_S14_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data51 = importdata('MIN_S15_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data52 = importdata('MIN_S16_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data53 = importdata('MIN_S17_R_Pelvis-Trunk Ang&AngVel.xlsx');
Data54 = importdata('MIN_S18_R_Pelvis-Trunk Ang&AngVel.xlsx');
%% 
save data
%load data
num=54;% 需要运行多少个数据 
Data=cell(num,1);
Data{1,1}=Data1;
Data{2,1}=Data2;
Data{3,1}=Data3;
Data{4,1}=Data4;
Data{5,1}=Data5;
Data{6,1}=Data6;
Data{7,1}=Data7;
Data{8,1}=Data8;
Data{9,1}=Data9;
Data{10,1}=Data10;
Data{11,1}=Data11;
Data{12,1}=Data12;
Data{13,1}=Data13;
Data{14,1}=Data14;
Data{15,1}=Data15;
Data{16,1}=Data16;
Data{17,1}=Data17;
Data{18,1}=Data18;
Data{19,1}=Data19;
Data{20,1}=Data20;
Data{21,1}=Data21;
Data{22,1}=Data22;
Data{23,1}=Data23;
Data{24,1}=Data24;
Data{25,1}=Data25;
Data{26,1}=Data26;
Data{27,1}=Data27;
Data{28,1}=Data28;
Data{29,1}=Data29;
Data{30,1}=Data30;
Data{31,1}=Data31;
Data{32,1}=Data32;
Data{33,1}=Data33;
Data{34,1}=Data34;
Data{35,1}=Data35;
Data{36,1}=Data36;
Data{37,1}=Data37;
Data{38,1}=Data38;
Data{39,1}=Data39;
Data{40,1}=Data40;
Data{41,1}=Data41;
Data{42,1}=Data42;
Data{43,1}=Data43;
Data{44,1}=Data44;
Data{45,1}=Data45;
Data{46,1}=Data46;
Data{47,1}=Data47;
Data{48,1}=Data48;
Data{49,1}=Data49;
Data{50,1}=Data50;
Data{51,1}=Data51;
Data{52,1}=Data52;
Data{53,1}=Data53;
Data{54,1}=Data54;
% 
%Data{10,1}=Data19;
%Data{11,1}=Data20;
%Data{12,1}=Data21;
for tt=1:num

f = fieldnames(Data{tt,1});
%% 1
y = {'\theta_1','\omega_1','\theta_2','\omega_2'};
%h = figure;
%set(h,'position',[100 100 800 480]);
for i = 1:4
    %subplot(4,4,(i-1)*4+1)
    temp = Data{tt,1}.(f{i})';
    aa=1:1:length(temp);
     temp=[ aa; temp];
   % plot(mapminmax(temp(1,:),0,1),temp(2,:),'k')
    %title(['RawData sheet',num2str(i)])
    %ylabel(y{i});
end
%% 2
theta1 = Data{tt,1}.(f{1});
omega1 = Data{tt,1}.(f{2});
theta2 = Data{tt,1}.(f{3});
omega2 = Data{tt,1}.(f{4});
theta1_ = 2*(theta1-min(theta1))./(max(theta1)-min(theta1))-1;
theta2_ = 2*(theta2-min(theta2))./(max(theta2)-min(theta2))-1;
omega1_ = omega1./(max(abs(omega1)));
omega2_ = omega2./(max(abs(omega2)));
%h = figure;
%set(h,'position',[100 100 700 240]);
%subplot(4,4,2)
%plot(theta1_,omega1_,'k')
%xlabel(['\theta_1^*'])
%ylabel(['\omega_1^*'])
%axis equal
%subplot(4,4,10)
%plot(theta2_,omega2_,'k')
%xlabel(['\theta_2^*'])
%ylabel(['\omega_2^*'])
%axis equal
%% 3
t = mapminmax(temp(1,:),0,1);
phi1 =atan2d(omega1_,theta1_);
phi2 =atan2d(omega2_,theta2_);
%h = figure;
%set(h,'position',[100 100 700 240]);
%subplot(4,4,7)
%plot(t,phi1,'k')
%ylabel(['\phi_1'])
%axis([0 1 -180 180])
%subplot(4,4,11)
%plot(t,phi2,'k')
%ylabel(['\phi_2'])
axis([0 1 -180 180])
% phase angel convert to degree by Hammil methood
for i=1:1:length(omega1_)
if omega1_(i)>0 & theta1_(i)>0
    newphi1(i)=phi1(i);
end
if omega1_(i)>0 & theta1_(i)<0
    newphi1(i)=phi1(i)+pi;
end
if omega1_(i)<0 & theta1_(i)<0
    newphi1(i)=pi-phi1(i);
end
if omega1_(i)<0 & theta1_(i)>0
    newphi1(i)=abs(phi1(i));
end
if omega2_(i)>0 & theta2_(i)>0
    newphi2(i)=(phi2(i));
end
if omega2_(i)>0 & theta2_(i)<0
    newphi2(i)=phi2(i)+pi;
end
if omega2_(i)<0 & theta2_(i)<0
    newphi2(i)=pi-phi2(i);
end
if omega2_(i)<0 & theta2_(i)>0
    newphi2(i)=abs(phi2(i));
end
end
temp_all(tt,:) = newphi2-newphi1;   %
%% 4
%h = figure;
%set(h,'position',[100 100 400 240]);
%subplot(4,4,8)
end

%% 
H13_MIN=mean(temp_all(1:18,:));
H13_MID=mean(temp_all(19:36,:));
H13_MAX=mean(temp_all(37:54,:));
% 如果加一个人 
%H13_MIN=mean(temp_all([1:3 10] ,:));
%H13_MID=mean(temp_all([4:6 11],:));
%H13_MAX=mean(temp_all([7:9 12],:));
figure()

plot(t,H13_MIN,'b','LineWidth',2)
hold on
plot(t,H13_MID,'k','LineWidth',2)
hold on
plot(t,H13_MAX,'r','LineWidth',2)
legend('Max Movement Distance','Mid Movement Distance','Min Movement Distance')
xlabel('% *** Phase','FontSize',20)
ylabel('*** CRP (Degree)','FontSize',30)
%temp(end-100:end)'